package com.zhongan.tb;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.table.AbstractTableModel;


public class TBTableModel extends AbstractTableModel {
    private static final long serialVersionUID = 1L;

    private Object[][] content;

    private String[] header;

    private NumberFormat formatter = new DecimalFormat("#.##");

    public TBTableModel(TBHelper helper) {

        List<TBMember> members = helper.getTeamgroup();
        this.header = new String[5];
//		this.header[0] ="�ݽ�˳��";
        this.header[0] = "���";
        this.header[1] = "����";
        this.header[2] = "�н����";
        this.header[3] = "��ע";
        this.header[4] = "ѡ��";
        this.content = new Object[members.size()][5];
        for (int i = 0; i < members.size(); i++) {
            Iterator<TBMember> iterator = members.iterator();
            TBMember member = new TBMember();
            while (iterator.hasNext()) {
                member = iterator.next();
//				if(member.getOrderNumber() == i+1)
                //ҳ��չʾ���������
                if (member.getId() == i + 1)
                    break;
            }
//			content[i][0] = member.getOrderNumber();
//			content[i][0] = "";
            content[i][0] = member.getId();
            content[i][1] = member.getName();
            content[i][2] = formatter.format(member.getFee());
            content[i][3] = member.getRemark();
            content[i][4] = false;
        }
    }


    @Override
    public int getRowCount() {
        return content.length;
    }

    @Override
    public int getColumnCount() {
        return header.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return content[rowIndex][columnIndex];
    }

    @Override
    public String getColumnName(int column) {
        return header[column];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return getValueAt(0, columnIndex).getClass();
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        content[rowIndex][columnIndex] = aValue;
        fireTableCellUpdated(rowIndex, columnIndex);
    }

    /**
     * ��ȡѡ�е��б��
     *
     * @return ѡ�е��еĳ�Ա���
     */
    public List<Integer> getSelectedId() {
        List<Integer> result = new ArrayList<>();
        int rowCount = getRowCount();
        for (int i = 0; i < rowCount; i++) {
            Object value_4 = getValueAt(i, 4);
            Object value_0 = getValueAt(i, 0);
            if (((Boolean) value_4).booleanValue()) {
                result.add(new Integer(value_0.toString()));
            }
        }
        return result;
    }
}
